/**
 * Copyright (c) 2014-2015, GoBelieve     
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package main

import "net"
import "time"
import "sync"
import "sync/atomic"
import log "github.com/golang/glog"
import "github.com/googollee/go-engine.io"

const CLIENT_TIMEOUT = (60 * 6)

type Connection struct {
	conn   interface{}
	closed int32
	
	forbidden int32 //是否被禁言
	notification_on bool //桌面在线时是否通知手机端
	online bool
	
	tc     int32 //write channel timeout count
	wt     chan *Message
	ewt    chan *EMessage //在线消息
	owt    chan *EMessage //离线消息

	//客户端协议版本号
	version int

	tm     time.Time
	appid  int64
	uid    int64
	device_id string
	device_ID int64 //generated by device_id + platform_id
	platform_id int8

	unackMessages map[int]*EMessage
	unacks map[int]int64
	mutex  sync.Mutex
}

func (client *Connection) SendGroupMessage(group_id int64, msg *Message) {
	appid := client.appid

	PushGroupMessage(appid, group_id, msg)
	group := group_manager.FindGroup(group_id)
	if group == nil {
		log.Warningf("can't send group message, appid:%d uid:%d cmd:%s", appid, group_id, Command(msg.cmd))
		return
	}

	route := app_route.FindRoute(appid)
	if route == nil {
		log.Warningf("can't send group message, appid:%d uid:%d cmd:%s", appid, group_id, Command(msg.cmd))
		return
	}

	members := group.Members()
	for member := range members {
	    clients := route.FindClientSet(member)
		if len(clients) == 0 {
			continue
		}

		for c, _ := range(clients) {
			if &c.Connection == client {
				continue
			}
			c.EnqueueMessage(msg)
		}
	}
}


func (client *Connection) SendMessage(uid int64, msg *Message) bool {
	appid := client.appid

	PushMessage(appid, uid, msg)

	route := app_route.FindRoute(appid)
	if route == nil {
		log.Warningf("can't send message, appid:%d uid:%d cmd:%s", appid, uid, Command(msg.cmd))
		return false
	}
	clients := route.FindClientSet(uid)
	if len(clients) == 0 {
		log.Warningf("can't send message, appid:%d uid:%d cmd:%s", appid, uid, Command(msg.cmd))
		return false
	}

	for c, _ := range(clients) {
		//不再发送给自己
		if &c.Connection == client {
			continue
		}
	
		c.EnqueueMessage(msg)
	}

	return true
}

func (client *Connection) EnqueueMessage(msg *Message) bool {
	closed := atomic.LoadInt32(&client.closed)
	if closed > 0 {
		log.Infof("can't send message to closed connection:%d", client.uid)
		return false
	}

	tc := atomic.LoadInt32(&client.tc)
	if tc > 0 {
		log.Infof("can't send message to blocked connection:%d", client.uid)
		atomic.AddInt32(&client.tc, 1)
		return false
	}
	select {
	case client.wt <- msg:
		return true
	case <- time.After(60*time.Second):
		atomic.AddInt32(&client.tc, 1)
		log.Infof("send message to wt timed out:%d", client.uid)
		return false
	}
}

func (client *Connection) EnqueueEMessage(emsg *EMessage) {
	closed := atomic.LoadInt32(&client.closed)
	if closed > 0 {
		log.Infof("can't send message to closed connection:%d", client.uid)
		return
	}

	tc := atomic.LoadInt32(&client.tc)
	if tc > 0 {
		log.Infof("can't send message to blocked connection:%d", client.uid)
		atomic.AddInt32(&client.tc, 1)
		return
	}
	select {
	case client.ewt <- emsg:
		break
	case <- time.After(60*time.Second):
		atomic.AddInt32(&client.tc, 1)
		log.Infof("send message to ewt timed out:%d", client.uid)
	}
}

func (client *Connection) EnqueueOfflineMessage(emsg *EMessage) {
	tc := atomic.LoadInt32(&client.tc)
	if tc > 0 {
		log.Infof("can't send message to blocked connection:%d", client.uid)
		atomic.AddInt32(&client.tc, 1)
		return
	}
	select {
	case client.owt <- emsg:
		break
	case <- time.After(60*time.Second):
		atomic.AddInt32(&client.tc, 1)
		log.Infof("send message to owt timed out:%d", client.uid)
	}
}


// 根据连接类型获取消息
func (client *Connection) read() *Message {
	if conn, ok := client.conn.(net.Conn); ok {
		conn.SetReadDeadline(time.Now().Add(CLIENT_TIMEOUT * time.Second))
		return ReceiveMessage(conn)
	} else if conn, ok := client.conn.(engineio.Conn); ok {
		return ReadEngineIOMessage(conn)
	}
	return nil
}

// 根据连接类型发送消息
func (client *Connection) send(msg *Message) {
	if conn, ok := client.conn.(net.Conn); ok {
		tc := atomic.LoadInt32(&client.tc)
		if tc > 0 {
			log.Info("can't write data to blocked socket")
			return
		}
		conn.SetWriteDeadline(time.Now().Add(60 * time.Second))
		err := SendMessage(conn, msg)
		if err != nil {
			atomic.AddInt32(&client.tc, 1)
			log.Info("send msg:", Command(msg.cmd),  " tcp err:", err)
		}
	} else if conn, ok := client.conn.(engineio.Conn); ok {
		SendEngineIOBinaryMessage(conn, msg)
	}
}

// 根据连接类型关闭
func (client *Connection) close() {
	if conn, ok := client.conn.(net.Conn); ok {
		conn.Close()
	} else if _, ok := client.conn.(engineio.Conn); ok {
		//bug:https://github.com/googollee/go-engine.io/issues/34
		//conn.Close()
	}
}
